<template>
  <div>
    <a-card style="margin-top: 12px;">
      <div>
        <a-form-model layout="horizontal" :model="form" ref="queryForm" :labelCol="{span: 7}" :wrapperCol="{span: 17, offset: 0}">
          <div class="fold">
            <a-row>
              <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <a-form-model-item
                  label="用户名"
                  prop="username"
                >
                  <a-input v-model="form.username" />
                </a-form-model-item>
              </a-col>
              <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <a-form-model-item
                  label="token"
                  prop="token"
                >
                  <a-input v-model="form.token" />
                </a-form-model-item>
              </a-col>
              <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <a-form-model-item
                  label="用户IP"
                  prop="userIp"
                >
                  <a-input v-model="form.userIp" />
                </a-form-model-item>
              </a-col>
              <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <a-form-model-item
                  label="目标系统IP"
                  prop="targetIp"
                >
                  <a-input v-model="form.targetIp" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row type="flex" justify="end">
              <a-button @click="submitForm" type="primary">查询</a-button>
              <a-button @click="resetForm" style="margin-left: 8px">重置</a-button>
            </a-row>
          </div>
        </a-form-model>
      </div>
    </a-card>
    <a-card style="margin-top: 12px;">
      <div>
        <div>
          <a-dropdown>
            <a-button class="space-vertical">批量操作 <a-icon type="down" /></a-button>
            <a-menu @click="batchAction" slot="overlay">
              <a-menu-item key="delete" v-auth="`monitor:onlineUser:delete`">批量强制下线</a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
        <standard-table
          ref="table"
          :size="`middle`"
          :page="page"
          :pageSize="pageSize"
          :total="total"
          :loading="loading"
          :columns="columns"
          :dataSource="dataSource"
          :selectedRows.sync="selectedRows"
          @onPageChange="onPageChange"
        >
          <div slot="action" slot-scope="{text, record}">
            <a @click="showDeleteModal(record.id)" v-auth="`monitor:onlineUser:delete`">
              强制下线
            </a>
          </div>
          <div slot="status">
            <a-badge status="success" text="在线" />
          </div>
        </standard-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import StandardTable from '@/components/table/standard-table'
import moment from 'moment'
import {onlineUserService} from '@/services'

const columns = [
  {
    title: '序号',
    dataIndex: 'no',
    width: 80,
    scopedSlots: { customRender: 'no' }
  },
  {
    title: '用户名',
    dataIndex: 'username',
    width: 140
  },
  {
    title: 'token',
    dataIndex: 'token',
    width: 180,
    ellipsis: true
  },
  {
    title: '用户IP',
    dataIndex: 'userIp',
    width: 140
  },
  {
    title: '目标系统IP',
    dataIndex: 'targetIp',
    width: 140,
		ellipsis: true
  },
  {
    title: '浏览器',
    dataIndex: 'browser',
    width: 160
  },
  {
    title: '操作系统',
    dataIndex: 'os',
    width: 180,
    ellipsis: true
  },
  {
    title: '登录时间',
    dataIndex: 'createTime',
    width: 160
  },
  {
    title: '最后访问时间',
    dataIndex: 'updateTime',
    width: 160
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    align: 'center',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    width: 120,
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'OnlineUser',
  components: {StandardTable},
  data () {
    return {
      loading: false,
      page: 1,
      pageSize: 10,
      total: 0,
      columns: columns,
      dataSource: [],
      selectedRows: [],
      form: {
        username: undefined,
        token: undefined,
        userIp: undefined,
        targetIp: undefined
      }
    }
  },
  authorize: {
    showDeleteModal: 'monitor:onlineUser:delete',
    showBatchDeleteModal: 'monitor:onlineUser:delete',
    exportAction: 'monitor:onlineUser:export'
  },
  methods: {
    moment,
    reloadTable() {
      this.selectedRows = []
      this.page = 1
      this.queryListAction()
    },
    onPageChange(pagination) {
      this.page = pagination.current
      this.pageSize = pagination.pageSize
      this.queryListAction()
    },
    resetForm() {
      this.$refs.queryForm.resetFields()
      this.reloadTable()
    },
    submitForm() {
      this.reloadTable()
    },
    deleteAction(keys) {
      this.confirmLoading = true
      const params = {
        ids: keys
      }
      return onlineUserService.del(JSON.stringify(params)).then(res => {
        const result = res.data
        if (result.code === 200) {
          this.$message.success(result.msg)
          this.selectedRows = []
          this.reloadTable()
        }
      }).finally(this.confirmLoading = false)
    },
    batchAction (e) {
      if (this.selectedRows.length <= 0) {
        this.$message.warning('请选择记录后操作')
        return
      }
      if (e.key === 'delete') {
        this.showBatchDeleteModal()
      }
    },
    showDeleteModal (key) {
      this.$confirm({
        confirmLoading: this.confirmLoading,
        icon: 'info-circle',
        title: '警告',
        content: '你确定要强制下线此用户吗？',
        okType: 'danger',
        onOk:() => {
          this.deleteAction([key])
        },
        onCancel() {},
      })
    },
    showBatchDeleteModal () {
      this.$confirm({
        confirmLoading: this.confirmLoading,
        icon: 'info-circle',
        title: '警告',
        content: '你确定要强制下线所有选中的用户吗？',
        okType: 'danger',
        onOk:() => {
          this.deleteAction(this.selectedRows.map(item => item.id))
        },
        onCancel() {},
      })
    },
    queryListAction() {
      this.loading = true
      const params = {
        page: this.page,
        pageSize: this.pageSize,
        queryParams: this.form
      }
      onlineUserService.queryList(JSON.stringify(params)).then(res => {
        const result = res.data
        if (result.code === 200) {
          this.dataSource = []
          this.dataSource = result.data.records
          this.total = result.data.totalCount
        }
      }).finally(this.loading = false)
    }
  },
  created() {
    this.reloadTable()
  }
}
</script>
<style scoped lang="less">
  .ant-form-item {
    margin-bottom: 12px;
  }
  .fold{
    width: calc(100% - 12px);
    display: inline-block;
  }
  @media screen and (max-width: 900px) {
    .fold {
      width: 100%;
    }
  }
</style>